package com.spcmg.controller;

import com.spcmg.service.ISpcMgDownloadService;
import com.spcmg.service.ISpcMgUploadService;
import com.spcmg.service.ISpcRoomService;
import com.spcmg.utils.JsonResult;
import com.spcmg.vo.StatStationRoomNumVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 机房表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-04-13
 */
@RestController
@RequestMapping("/spc/room")
public class SpcRoomController {

    @Resource
    private ISpcRoomService service;

    @Resource
    private ISpcMgUploadService spcMgUploadService;

    @Resource
    private ISpcMgDownloadService spcMgDownloadService;

    @GetMapping("/test")
    public JsonResult test() {
        return JsonResult.SUCCESS;
    }

    /**
     * 增加机房
     *
     * @param chinaName       机房名
     * @param stationId       局站Id
     * @param address         机房地址
     * @param street          所属街道、乡镇
     * @param x               经度
     * @param y               纬度
     * @param area            建筑面积
     * @param usedArea        已用面积
     * @param toHireBack      可出租机架数
     * @param hiredRack       已出租机架数
     * @param length          长
     * @param width           宽
     * @param height          高
     * @param classId         机房等级
     * @param typeId          机房类型
     * @param roomTypeDetail  机房类型细类
     * @param startUseDate    投产时间
     * @param maintenanceUnit 维护单位
     * @param flag            是否基站机房 是（1）/否（0）
     * @param notes           备注
     * @return 返回数据
     */
    @PostMapping("/insert")
    public JsonResult insertRoom(@RequestParam String chinaName,
                                 @RequestParam String stationId,
                                 @RequestParam String address,
                                 @RequestParam(required = false) String street,
                                 @RequestParam String x,
                                 @RequestParam String y,
                                 @RequestParam(required = false) String area,
                                 @RequestParam(required = false) String usedArea,
                                 @RequestParam(required = false) Integer toHireBack,
                                 @RequestParam(required = false) Integer hiredRack,
                                 @RequestParam(required = false) String length,
                                 @RequestParam(required = false) String width,
                                 @RequestParam(required = false) String height,
                                 @RequestParam Integer classId,
                                 @RequestParam Integer typeId,
                                 @RequestParam(required = false) Integer roomTypeDetail,
                                 @RequestParam String startUseDate,
                                 @RequestParam(required = false) String maintenanceUnit,
                                 @RequestParam(required = false) Integer flag,
                                 @RequestParam(required = false) String notes) {
        return service.insertRoom(chinaName, stationId, address, street, x, y, area, usedArea, toHireBack, hiredRack,
                length, width, height, classId, typeId, roomTypeDetail, startUseDate, maintenanceUnit, flag, notes);
    }

    /**
     * 导入机房
     *
     * @param file           文件
     * @param handlePerTimes 每次处理数量
     * @return
     */
    @PostMapping("/upload")
    public void upload(@RequestParam("file") MultipartFile file,
                       @RequestParam(required = false) Integer handlePerTimes,
                       HttpServletResponse res) throws Exception {
        if (handlePerTimes == null || handlePerTimes <= 0) {
            handlePerTimes = 100000;
        }
        spcMgUploadService.upload(file, handlePerTimes, res);
    }

    /**
     * 导出所有机房信息Post
     *
     * @param res
     */
    @PostMapping("/download")
    public void download(HttpServletResponse res) throws Exception {
        spcMgDownloadService.download(res);
    }

    /**
     * 导出所有机房信息Get
     *
     * @param res
     */
    @GetMapping("/download1")
    public void download1(HttpServletResponse res) throws Exception {
        spcMgDownloadService.download(res);
    }

    /**
     * 统计对应局站内所有“机房等级”内所有“机房类型”的机房数量
     *
     * @param stationId
     * @return
     */
    @PostMapping("/stat/station/room/num")
    public JsonResult statStationRoomNum(Long stationId) {
        List<StatStationRoomNumVO> list = service.statStationRoomNum(stationId);
        return JsonResult.success()
                .data(list)
                .build();
    }

    /**
     * 根据条件查询机房
     *
     * @param type 查询类型 1=机房Id 2=等于机房名 3=模糊查询机房名 4=模糊查询所属局站名
     * @param condition 条件
     * @return
     * @throws Exception
     */
    @PostMapping("/select/by/condition")
    public JsonResult selectSpcRoom(@RequestParam Integer type,
                                    @RequestParam String condition) throws Exception {
        return service.selectSpcRoom(type, condition);
    }
}
